package com.woniuxy.lrz.carlproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.util.BeanCopyUtil;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarBrandFindForm;
import com.woniuxy.lrz.carlproject.dao.CarDetailMapper;
import com.woniuxy.lrz.carlproject.dao.CarTypeMapper;
import com.woniuxy.lrz.carlproject.model.CarBrand;
import com.woniuxy.lrz.carlproject.dao.CarBrandMapper;
import com.woniuxy.lrz.carlproject.model.CarDetail;
import com.woniuxy.lrz.carlproject.model.CarType;
import com.woniuxy.lrz.carlproject.service.CarBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarBrandDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 汽车品牌表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
@Slf4j
public class CarBrandServiceImpl extends ServiceImpl<CarBrandMapper, CarBrand> implements CarBrandService {
    @Resource
    private CarBrandMapper carBrandMapper;
    @Resource
    private CarTypeMapper carTypeMapper;
    @Resource
    private CarDetailMapper carDetailMapper;
    @Override
    public boolean addOrUpdateCarBrand(CarAddOrUpdateForm carAddOrUpdateForm) {
            CarBrand carBrand = BeanUtil.copyProperties(carAddOrUpdateForm, CarBrand.class);
            //判断操作是新增品牌还是修改品牌
            CarBrand car_brand_name = carBrandMapper.selectOne(new QueryWrapper<CarBrand>().eq("car_brand_name", carBrand.getCarBrandName()));
            if(carAddOrUpdateForm.getCarBrandId()==null){
                //新增品牌
                if(car_brand_name!=null) throw new AppException("品牌信息重复，请重试！");
                carBrandMapper.insert(carBrand);
            }
            //修改品牌
            if(car_brand_name!=null&&car_brand_name.getCarBrandId()!=carBrand.getCarBrandId()) throw new AppException("品牌信息重复，请重试！");
            carBrandMapper.updateById(carBrand);



//        //新增车型
//        ArrayList<CarTypeAddOrUpdateForm> carTypeAddItems = carAddOrUpdateForm.getCarTypeAddItems();
//        if(carTypeAddItems!=null&&carTypeAddItems.size()>0){
//            for (CarTypeAddOrUpdateForm carTypeAddItem : carTypeAddItems) {
//                CarType carType = BeanUtil.copyProperties(carTypeAddItem, CarType.class);
//                carType.setCarBrandId(carBrand.getCarBrandId());
//                carTypeMapper.insert(carType);
//                //新增详细信息
//                ArrayList<CarDetailsAddOrUpdateForm> carDetailsAddOrUpdateForms = carTypeAddItem.getCarDetailsAddOrUpdateForms();
//                if(carDetailsAddOrUpdateForms!=null&&carDetailsAddOrUpdateForms.size()>0){
//                    for (CarDetailsAddOrUpdateForm carDetailsAddItem : carDetailsAddOrUpdateForms) {
//                        CarDetail carDetail = BeanUtil.copyProperties(carDetailsAddItem, CarDetail.class);
//                        carDetail.setCarTypeId(carType.getCarTypeId());
//                        carDetailMapper.insert(carDetail);
//                    }
//                }
//
//            }
//        }

        return true;
    }

    @Override
    public boolean deleteCarBrand(Integer carBrandId) {
        //检查是不是重复删除
        CarBrand carBrand = carBrandMapper.selectById(carBrandId);
        if(carBrand==null){
            throw new AppException("请勿重复删除！");
        }
        //执行删除操作
        carBrandMapper.deleteById(carBrandId);
        //删除对应品牌的车型
        List<CarType> car_brand_id = carTypeMapper.selectList(new QueryWrapper<CarType>().eq("car_brand_id", carBrandId));
        for (CarType carType : car_brand_id) {
            //删除对应车型的具体信息
            carDetailMapper.delete(new QueryWrapper<CarDetail>().eq("car_type_id",carType.getCarTypeId()));
        }
        carTypeMapper.delete(new QueryWrapper<CarType>().eq("car_brand_id", carBrandId));
        return true;
    }

    @Override
    public Page<CarBrandDto> findCarBrand(CarBrandFindForm carBrandFindForm) {
        Page<CarBrand> page=new Page<>(carBrandFindForm.getPageNum(),carBrandFindForm.getPageSize());
        if(carBrandFindForm.getBrand()!=null&&!carBrandFindForm.getBrand().equals("")){
            Page<CarBrand> car_brand_name = carBrandMapper.selectPage(page, new QueryWrapper<CarBrand>().like("car_brand_name", carBrandFindForm.getBrand()));
            Page<CarBrandDto> carBrandDtoPage = BeanCopyUtil.copyPageInfo(car_brand_name, CarBrandDto.class);
            return carBrandDtoPage;
        }
        Page<CarBrand> car_brand_name = carBrandMapper.selectPage(page,new QueryWrapper<>());
        Page<CarBrandDto> carBrandDtoPage = BeanCopyUtil.copyPageInfo(car_brand_name, CarBrandDto.class);
        return carBrandDtoPage;
    }
}
